package api

import (
	"context"
	"gdmin/servlet"
	utils2 "gdmin/utils"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/cloudwego/hertz/pkg/route"
	"strconv"
)

func beatUser(c context.Context, ctx *app.RequestContext) {
	hlog.Info("测试用户接口是否可使用")

	ctx.JSON(consts.StatusOK, utils.H{"message": "user_api"})
	return

}

// 创建用户
// @Tags      用户
// @Summary   新增用户
// @accept    application/json
// @Produce   application/json
// @Param     data  body      servlet.CreateUserReq             true  "创建参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.User}  "成功"
// @Router    /user/save [post]
func createUser(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateUserReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	d, err := userService.CreateUser(&req)
	hlog.Info("user = ", d)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &d)
	return

}

// 修改用户
// @Tags      用户
// @Summary   修改用户
// @accept    application/json
// @Produce   application/json
// @Param     data  body      servlet.UpdateUserReq             true  "修改参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.User}  "成功"
// @Router    /user/update [post]
func updateUser(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateUserReq
	err := c.BindAndValidate(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	user, err := userService.UpdateUser(&req)
	hlog.Info("user = ", user)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, &user)
	return

}

// 分页查询用户
// @Tags      用户
// @Summary   分页查询用户
// @Produce   application/json
// @Param     name  query       string false  "姓名"
// @Param     login_name  query       string false  "登录名"
// @Param     phone  query       string false  "手机"
// @Param     sex  query       string false  "性别"
// @Param     page_size  query       string false  "页面行数"
// @Param     page_index  query       string false  "页面起始位置"
// @Param     sort  query       string false  "排序规则. 案例: create_time asc , id desc"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.UserResp}  "成功"
// @Router    /user/page [post]
func queryUser(ctx context.Context, c *app.RequestContext) {
	var req servlet.QueryUserReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, userService.QueryUser(req))
	return
}

// 用户列表
// @Tags      用户
// @Summary   用户列表
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.UserResp}  "成功"
// @Router    /user/list [post]
func listUser(ctx context.Context, c *app.RequestContext) {
	servlet.OkWithData(c, userService.List())
	return
}

func UserApi(r *route.RouterGroup) {
	r.GET("user/beat", beatUser)
	r.POST("/user/save", createUser)
	r.POST("/user/update", updateUser)
	r.POST("/user/page", queryUser)
	r.POST("/user/list", listUser)
	r.POST("/user/:id", idUser)
	r.POST("/user/info", userInfo)
	r.POST("/user/delete/:id", userDel)

}

// 根据id查询用户
// @Tags      用户
// @Summary   根据id查询用户
// @Produce   application/json
// @Param id path int true "用户id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.UserResp}  "成功"
// @Router    /user/delete/:id [post]
func userDel(ctx context.Context, c *app.RequestContext) {
	err := userService.DeleteById(c.Param("id"))
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.Ok(c)
	return
}

// 用户信息
// @Tags      用户
// @Summary   用户信息
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.Userinfo}  "成功"
// @Router    /user/info [post]
func userInfo(ctx context.Context, c *app.RequestContext) {
	id := utils2.GetThreadUserId()
	if id > 0 {

		byId := userService.QueryUserById(strconv.Itoa(id))
		if byId != nil {
			var resp servlet.Userinfo

			resp.Name = byId.Name
			resp.Phone = byId.Phone
			resp.Email = byId.Email
			resp.Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"

			resp.Role = roleService.FindByUserIdWithName(id, &resp)
			resp.Menus = resService.FindMenuByRoleIds(resp.RoleIds)
			servlet.OkWithData(c, &resp)
			return
		}
	}

	return

}

// 根据id查询用户
// @Tags      用户
// @Summary   根据id查询用户
// @Produce   application/json
// @Param id path int true "用户id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.UserResp}  "成功"
// @Router    /user/:id [post]
func idUser(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	servlet.OkWithData(c, userService.QueryUserById(param))
	return

}
